<?php
	if(!$_SESSION['sesProcessed']){
			   
		if(!$chkTerms){
			array_push($ActionResults, "Please confirm below that you have read the terms and conditions.");
			$intFailed++;
		
		}
		 
		if(!$txtShipCountry) {
			array_push($ActionResults, "Please enter country.");
			$intFailed++;
		}
	
		if(!$txtShipPhone) {
			array_push($ActionResults, "Please enter phone number as we need to call you for credit card details.");
			$intFailed++;
		}
		
		/*
		if(!$txtShipRegion) {
			array_push($ActionResults, "Please enter county or state.");
			$intFailed++;
		}
		*/
		
		if(!$txtShipPostalCode) {
			array_push($ActionResults, "A valid Post code is required to process order.");
			$intFailed++;
		}
		
		if(!$txtShipCity) {
			array_push($ActionResults, "Please enter town/city.");
			$intFailed++;
		}
	
		
		if(!$txtShipAddress) {
			array_push($ActionResults, "Please enter address.");
			$intFailed++;
		}
	
		
		if(!$txtShipName) {
			array_push($ActionResults, "No first name supplied!");
			$intFailed++;
		}

		
		
		
		//check for delivey address
		if(SEPARATE_DELIVERY_ADDRESS){
						
			//DeliveryAddress
			     if(!$txtDeliveryAddress) {
			         array_push($ActionResults, "Please enter Delivery Address.");
			         $intFailed++;
			     }else{
			         $txtDeliveryAddress = ucwords(strtolower($txtDeliveryAddress)); 
			     }
			
			//DeliveryCity
			     if(!$txtDeliveryCity) {
			         array_push($ActionResults, "Please enter Delivery City.");
			         $intFailed++;
			     }else{
			         $txtDeliveryCity = ucwords(strtolower($txtDeliveryCity)); 
			     }
			
			//DeliveryRegion
			     if(!$txtDeliveryRegion) {
			         array_push($ActionResults, "Please enter Delivery Region.");
			         $intFailed++;
			     }else{
			         $txtDeliveryRegion = ucwords(strtolower($txtDeliveryRegion)); 
			     }
			
			//DeliveryPostalCode
			     if(!$txtDeliveryPostalCode) {
			         array_push($ActionResults, "Please enter Delivery Postal Code.");
			         $intFailed++;
			     }else{
			         $txtDeliveryPostalCode = strtoupper($txtDeliveryPostalCode); 
			     }
		}
		
		
		
		 if(EXTRA_ORDER_FIELD) {
		 	if(!$txtOtherInfo) {
				array_push($ActionResults, "Please enter ".EXTRA_ORDER_FIELD);
	         	$intFailed++;
			}
		 }
		 

		
		
		/*************  Card procssing  *************/
		if(CARD_PROCESSING){
			/*
				if($cboCardType == 4){
					if(!$cboIssueNo) {
						array_push($ActionResults, "Please enter issue number for Switch Card.");
						$intFailed++;
					}
				}
			*/
			/*
			if(!$cboStartYear) {
				array_push($ActionResults, "Please enter Year card is valid from.");
				$intFailed++;
			}
			
			if(!$cboStartMonth) {
				array_push($ActionResults, "Please enter Month card is valid from.");
				$intFailed++;
			}
			*/
			if(!$cboExpiryYear) {
				array_push($ActionResults, "Please enter Year card expires.");
				$intFailed++;
			}
			
			if(!$cboExpiryMonth) {
				array_push($ActionResults, "Please enter Month card expires.");
				$intFailed++;
			}
			
			if($txtCardNumber)	{
				list ($boValid, $strType) = credit_card::check ($txtCardNumber);
				
	    			if(!$boValid){
						array_push($ActionResults, "Invalid $strType Card Number.");
						$intFailed++;
					} // Displays 1 (TRUE)
					
				//array_push($ActionResults, "Card Type $strType.");

					
			}else{
				array_push($ActionResults, "Please Enter Card Number.");
				$intFailed++;
			}
			
			if((!isset($_REQUEST['txtCSCNumber'])) || ($_REQUEST['txtCSCNumber'] == "") ){
				array_push($ActionResults, "Please Enter CSC Number.");
				$intFailed++;
			}
			
			if(!$cboCardType){
					array_push($ActionResults, "Please select card type.");
					$intFailed++;
			}
		}
		/*************  End Card procssing ******************/
	
		if($intFailed > 0){
			$SCREEN = "scrCheckout";
			return(FALSE);
		}
		
		
		include (MODULE_SHOP_HOST . "/navigation/EmailHeader");
		//add Order to tblOrders
		include (MODULE . "/action/qryAddOrders");
		$_SESSION['sesOrderID'] = mysql_insert_id();
		//session_register(sesOrderID);
		//echo $_SESSION['sesOrderID']." order ID";
		if($intFailed > 0){
			$SCREEN = "scrCheckout";
			return(FALSE);
		}
		
		/*************  Card procssing  *************/
		/*
		Card Details not stored in database.
		if(CARD_PROCESSING){
			//Add Card Details to tblCardDetails
			//include (MODULE . "/action/qryAddCardDetails");
	
			if($intFailed > 0){
				$SCREEN = "scrCheckout";
				return(FALSE);
			}
		}
		*/
		/*************  End Card procssing  *************/




		//Process shopping cart and add to order details
		 $session_array = explode(";",session_encode()); 
		 
			for ($x = 0; $x < count($session_array); $x++) { 
				//echo "adding details<br>";
				if (substr($session_array[$x],0,7)=="sesItem"){
	
				//if (eregi("(sesItem)([0-9]{1,9})(\|i\:)([0-9]{1,9})$",$session_array[$x], $OrderDetail)){	
				if (eregi("(sesItem)([0-9]{1,9})(\_)([0-9]{1,9})(\_)([0-9]{1,9})(\|i\:)([0-9]{1,9})$",$session_array[$x], $OrderDetail)){	
					
					/*	
						$strhtml .= "0 - $OrderDetail[0] \n<br>"; 
						$strhtml .= "1 - $OrderDetail[1] \n<br>"; 
						$strhtml .= "2 -Product ID $OrderDetail[2] \n<br>";
						$strhtml .= "3 - $OrderDetail[3] \n<br>";  
						$strhtml .= "4 -Colour  $OrderDetail[4] \n<br>";  
						$strhtml .= "5 - $OrderDetail[5] \n<br>";  
						$strhtml .= "6 -Size  $OrderDetail[6] \n<br>"; 
						$strhtml .= "7 - $OrderDetail[7] \n<br>";  
						$strhtml .= "8 -Quantity  $OrderDetail[8] \n<br>";
					//*/
						// Get price of items
						$strSQL = "SELECT UnitPrice, VatRate FROM tblProduct
									WHERE tblProduct.ID =  '$OrderDetail[2]'";
						include (MODULE . "/action/actMySqlResults");
						
						$intUnitPrice = $myrow["UnitPrice"];
						$intVatRate = $myrow["VatRate"];
						
						$strSQL = "INSERT INTO tblOrderDetails
				                 ( tblOrderDetails.OrderHeader
								, tblOrderDetails.Product
					            , tblOrderDetails.UnitPrice
					            , tblOrderDetails.Quantity
								, tblOrderDetails.Size
								, tblOrderDetails.Colour
								, tblOrderDetails.VatRate)
						Values( '".$_SESSION['sesOrderID']."'
								, '$OrderDetail[2]'
					            , '$intUnitPrice'
					            , '$OrderDetail[8]'
								, '$OrderDetail[6]'
								, '$OrderDetail[4]'
								, '$intVatRate')";
						//echo $strSQL;
						include (MODULE . "/action/actMySqlResults");
						
							if($intFailed > 0){
								$SCREEN = "scrCheckout";
								return(FALSE);
							}else{
								echo "<!-- session variable ". $OrderDetail[1].$OrderDetail[2].$OrderDetail[3].$OrderDetail[4].$OrderDetail[5].$OrderDetail[6]."-->";
								//remove item from basket
								unset($_SESSION[$OrderDetail[1].$OrderDetail[2].$OrderDetail[3].$OrderDetail[4].$OrderDetail[5].$OrderDetail[6]]);
							}
					}
				}
			}
			
			//Send Email of order to retailer
			$intOrderHeaderID = $_SESSION['sesOrderID'];
			include (MODULE . "/action/qrySelectOrderHeader");
			$strFromEmail = EMAIL_ORDER;
			$strFromName = "";
			$strToEmail = EMAIL_ORDER;
			$strSubject = "New Order number ".$_SESSION['sesOrderID'].". Placed by ".$myrow["ShipName"]." ".$_SESSION['sesUserName'];
			//$strEmailBCC is set in the config file

			// Add $strHeader
			include (MODULE . "/screen/OrderDetails");
			SendMime($strSubject, $strMessageBody, $strToEmail, $strFromEmail, $strFromName,$strHeader,"",$strEmailBCC);
			 	
				//Send order confirmation to customer
				include (MODULE . "/action/qrySelectOrderHeader");
					$strFromEmail = EMAIL_ORDER;
					$strFromName = "";
					$strToEmail = $_SESSION['sesUserName'];
					$strSubject = SITE_NAME." : Order number ".$_SESSION['sesOrderID'];

			// Add $strHeader
			include (MODULE . "/screen/OrderDetails");
			SendMime($strSubject, $strMessageBody, $strToEmail, $strFromEmail, $strFromName,$strHeader);
	
		if(CARD_PROCESSING){
			include (MODULE . "/screen/CreditCardDetails.php");
			$strFromEmail = EMAIL_ORDER;
			$strFromName = "orders";
			$strToEmail = EMAIL_ORDER;
			$strSubject = SITE_NAME." : Order number ".$_SESSION['sesOrderID'];
			
			if($strCreditCardEmail == "")
				$strCreditCardEmail = $strToEmail;	
			
			// Add $strHeader
			SendMime($strSubject, $strMessageBody, $strCreditCardEmail, $strFromEmail, $strFromName,$strHeader);

		}
		
		$_SESSION['sesProcessed'] = "true";
		//session_register(sesProcessed);
	}else{
		//come straight to here if order has already been added
	}

?>
